package com.wm.wmim.server.rpcinvoke;

import com.alibaba.fastjson.JSONObject;
import com.wm.wmim.common.dto.RpcRespDTO;
import com.wm.wmim.common.utils.RpcRespUtils;
import com.wm.wmim.connect.sdk.push.bo.req.PushWsMessageReqBO;
import com.wm.wmim.connect.sdk.push.bo.resp.PushWsMessageRespBO;
import com.wm.wmim.connect.sdk.push.enums.WsPushResultEnum;
import com.wm.wmim.connect.sdk.push.rpc.PushSdkService;
import com.wm.wmim.server.dto.bo.WsMessageBO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author weiming
 * @date 2024/10/19 14:14
 * @description
 */
@Service
public class PushWsMessageRpcInvokeService {

    @DubboReference(version = "1.0.0", check = false)
    private PushSdkService pushSdkService;

    public Boolean push(Long userId, WsMessageBO messageBO) {
        PushWsMessageReqBO pushWsMessageReqBO = new PushWsMessageReqBO();
        pushWsMessageReqBO.setUserId(userId);
        pushWsMessageReqBO.setMessage(JSONObject.toJSONString(messageBO));
        pushWsMessageReqBO.setBusinessScene("im");
        RpcRespDTO<PushWsMessageRespBO> pushWsMessageRespBORpcRespDTO = pushSdkService.pushMessage(pushWsMessageReqBO);
        PushWsMessageRespBO pushWsMessageRespBO = RpcRespUtils.getData(pushWsMessageRespBORpcRespDTO);
        WsPushResultEnum pushResult = pushWsMessageRespBO.getPushResult();
        if (StringUtils.equals(pushResult.getCode(), WsPushResultEnum.ALL_FAIL.getCode())) {
            return false;
        }
        return true;
    }
}
